﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.ComponentModel;

namespace produccion.clases
{
    class CLSENCUNI : INotifyPropertyChanged
    {
        MDOgeneral mod = new MDOgeneral();
        string coduni_eun, nomuni_eun;

        public string CODUNI_EUN
        {
            get { return coduni_eun; }
            set { coduni_eun = value; }
        }

        public string NOMUNI_EUN
        {
            get { return nomuni_eun; }
            set { nomuni_eun = value; }
        }

        public void LimpiarValores()
        {
            NOMUNI_EUN = "";
            CODUNI_EUN = "";
        }

        public Boolean ExisteUnidad()
        {
            try
            {
                string query = "select coduni_eun, nomuni_eun from encunip where coduni_eun='" + CODUNI_EUN + "'";
                if (mod.consulta(query).Rows.Count > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable ConsultarTodas()
        {
            try
            {
                string query = "select coduni_eun as codigo, nomuni_eun as nombre from encunip order by coduni_eun, nomuni_eun";
                DataTable dt = mod.consulta(query);
                if (dt.Rows.Count.Equals(0))
                {
                    query = "select '' as coduni_eun, '' as nomuni_eun from encunip";
                    dt = mod.consulta(query);
                    return dt;
                }
                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public string TraerMaximoCod()
        {
            try
            {
                string query = "select max(coduni_eun) + 1 from encunip";
                DataTable dt = mod.consulta(query);
                if (dt.Rows.Count.Equals(0))
                {
                    return "1";
                }
                return dt.Rows[0][0].ToString() ;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable Consultar()
        {
            try
            {
                string query = "select nomuni_eun from encunip where coduni_eun='" + CODUNI_EUN + "'";
                DataTable dt = mod.consulta(query);
                if (dt.Rows.Count > 0)
                {
                    NOMUNI_EUN = dt.Rows[0][0].ToString().Trim();
                }
                else
                {
                    NOMUNI_EUN = "";
                    CODUNI_EUN = "";
                }
                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int Eliminar()
        {
            try
            {
                string query = "delete from encunip where coduni_eun='" + CODUNI_EUN + "'";
                return mod.comando(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int Actualizar()
        {
            try
            {
                string query = "update encunip set nomuni_eun='" + NOMUNI_EUN + "' where coduni_eun='" + CODUNI_EUN + "'";
                return mod.comando(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int Insertar()
        {
            try
            {
                string query = "insert into encunip (coduni_eun, nomuni_eun) values ('" + CODUNI_EUN + "','" + NOMUNI_EUN + "')";
                return mod.comando(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public DataTable Buscar()
        {
            try
            {
                string query = "select coduni_eun as codigo, nomuni_eun as nombre from encunip where nomuni_eun like '%" + NOMUNI_EUN + "%' order by nomuni_eun asc";
                return mod.consulta(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public Boolean ExisteMaeMat()
        {
            try
            {
                string query = "select codmat_mat from maemat where uniusu_mat ='" + CODUNI_EUN + "' or unicom_mat='" + CODUNI_EUN + "'";
                DataTable dt = mod.consulta(query);
                if (dt.Rows.Count > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

    }
}
